package com.ly.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ly.bean.CarItem;
import com.ly.bean.EasybuyProduct;
import com.ly.bean.EasybuyProductImages;
import com.ly.bean.EasybuyUser;
import com.ly.mapper.EasybuyProductImagesMapper;
import com.ly.mapper.EasybuyProductMapper;
import com.ly.service.ProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
@author 李岩
@date 2021/12/14-13:52
@describe
*/
@Component
@Service(interfaceClass = ProductService.class)
public class ProductServiceImpl implements ProductService {
    private final Logger logger= LoggerFactory.getLogger(ProductServiceImpl.class);
    private ProductServiceImpl(){
        System.out.println("ProductServiceImpl   商品的service层");
    }
    @Autowired
    private EasybuyProductMapper productMapper;
    @Autowired
    private EasybuyProductImagesMapper productImagesMapper;
    @Autowired
    private RedisTemplate redisTemplate;


    //卖家添加商品信息以及商品详情
    @Override
    public void addProduct(EasybuyProduct product, List<String> productImageString) throws Exception {
        logger.info("卖家添加商品信息以及图片信息");
        //添加商品
        productMapper.insertSelective(product);
        for (String img : productImageString) {
            EasybuyProductImages productImages = new EasybuyProductImages();
            productImages.setFilename(img);
            productImages.setPid(product.getId());
            productImagesMapper.insertSelective(productImages);
        }

    }
    /**
     * 获取前六件商品
     */
    @Override
    public List<EasybuyProduct> selectSixByCategoryLevel1Id(Integer categoryLevel1Id) throws Exception {
        return productMapper.selectSixByCategoryLevel1Id(categoryLevel1Id);
    }
    /**
     * 根据商品id查询该商品信息
     */
    @Override
    public EasybuyProduct selectProductByPrimaryKey(Integer id) throws Exception {
        return productMapper.selectProductByPrimaryKey(id);
    }
    /**
     * 把商品信息添加到redis中
     */
    @Override
    public void addProductCarToRedis(EasybuyProduct product, EasybuyUser user, Integer buyNum) throws Exception {
        //使用hash存储购物车信息  hash<Key,Map<Filed,Value>> Key是用户id Filed为商品id Value是商品信息
        //如果存在该商品那么直接在数量上加
        if (redisTemplate.boundHashOps("user_car_"+user.getId()).hasKey(product.getId()+"")){
            CarItem carItem =(CarItem) redisTemplate.boundHashOps("user_car_" + user.getId()).get(product.getId() + "");
            carItem.setBuyNum(carItem.getBuyNum()+buyNum);
            redisTemplate.boundHashOps("user_car_" + user.getId()).put(product.getId() + "",carItem);
        }
        //如果不存在 则为第一次购买  创建购物车放到redis中
        CarItem carItem = new CarItem();
        carItem.setEasybuyProduct(product);
        carItem.setBuyNum(buyNum);
        redisTemplate.boundHashOps("user_car_" + user.getId()).put(product.getId() + "",carItem);
    }
    //从redis中获取该用户所有的购物车信息
    @Override
    public List<CarItem> findProductCarItemFromRedis(EasybuyUser loginUser) throws Exception{
        logger.info("利用用户id从redis中获取该用户所有的购物车信息");
        List<CarItem> carItemList = redisTemplate.boundHashOps("user_car_" + loginUser.getId()).values();
        return carItemList;
    }
    //从redis中获取该用户所有的购物车信息
    @Override
    public void deleteProductCarItemFromRedis(EasybuyUser loginUser, Integer pid) throws Exception {
        redisTemplate.boundHashOps("user_car_" + loginUser.getId()).delete(pid + "");
    }
    //利用用户id和商品id从redis中获取确定购买的商品信息
    @Override
    public CarItem findOrderCarItemFromRedis(Integer uid,Integer pid) throws Exception{
        logger.info("利用用户id和商品id从redis中获取确定购买的商品信息");
        return (CarItem) redisTemplate.boundHashOps("user_car_" + uid).get(pid + "");
    }
    //根据登录卖家的信息，获取卖家中售卖的商品列表
    @Override
    public PageInfo<EasybuyProduct> selectProductBySellerId(String pageNum,String sellerId) throws Exception {
        Integer pageNumInt=1;
        Integer pageSizeInt=5;
        if (pageNum!=null && !"".equals(pageNum)){
            pageNumInt=Integer.valueOf(pageNum);
        }
        PageHelper.startPage(pageNumInt,pageSizeInt);
        List<EasybuyProduct> productList = productMapper.selectProductBySellerId(sellerId);
        PageInfo<EasybuyProduct> pageInfo = new PageInfo<>(productList);
        return pageInfo;
    }
}
